﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace MLBService.Resources
{
    [DataContract]
    public class BattingStatsItem
    {
        #region Properties
        
        [DataMember]
        public UriLink PlayerItem { get; set; }
        [DataMember]
        public List<UriLink> Teams = new List<UriLink>();
        [DataMember]
        public List<int?> Year = new List<int?>();
        [DataMember]
        public List<short?> Stint = new List<short?>();
        [DataMember]
        public List<short?> G = new List<short?>();
        [DataMember]
        public List<short?> GB = new List<short?>();
        [DataMember]
        public List<short?> AB = new List<short?>();
        [DataMember]
        public List<short?> R = new List<short?>();
        [DataMember]
        public List<short?> H = new List<short?>();
        [DataMember]
        public List<short?> Doubles = new List<short?>();
        [DataMember]
        public List<short?> Triples = new List<short?>();
        [DataMember]
        public List<short?> HR = new List<short?>();
        [DataMember]
        public List<short?> RBI = new List<short?>();
        [DataMember]
        public List<short?> SB = new List<short?>();
        [DataMember]
        public List<short?> CS = new List<short?>();
        [DataMember]
        public List<short?> BB = new List<short?>();
        [DataMember]
        public List<short?> SO = new List<short?>();
        [DataMember]
        public List<short?> IBB = new List<short?>();
        [DataMember]
        public List<short?> HBP = new List<short?>();
        [DataMember]
        public List<short?> SH = new List<short?>();
        [DataMember]
        public List<short?> SF = new List<short?>();
        [DataMember]
        public List<short?> GIDP = new List<short?>();

        #endregion

        public BattingStatsItem Load(int playerid, int year)
        {
            using (Model.fourseamEntities context = new Model.fourseamEntities())
            {
                var s = (
                    from p in context.People
                    from a in context.Appearances
                    from ft in context.FieldedTeams
                    from nn in context.TeamNickNames
                    from bs in context.BattingStats
                    where a.PersonId == p.PersonId &&
                        ft.FieldedTeamId == a.TeamId &&
                        nn.TeamNickNameId == ft.TeamNickNameId &&
                        bs.TeamId == ft.FieldedTeamId &&
                        p.PersonId == playerid &&
                        ft.Yr == year &&
                        bs.PersonId == p.PersonId
                    orderby bs.Stint
                    select new
                    {
                        p.FirstName,
                        p.LastName,
                        p.PersonId,
                        ft.Yr,
                        nn.NickName,
                        ft.FieldedTeamId,
                        bs.AB,
                        bs.BattingStatId,
                        bs.BB,
                        bs.CS,
                        bs.Doubles,
                        bs.G,
                        bs.G_Batting,
                        bs.G_old,
                        bs.GIDP,
                        bs.H,
                        bs.HBP,
                        bs.HR,
                        bs.IBB,
                        bs.R,
                        bs.RBI,
                        bs.SB,
                        bs.SF,
                        bs.SH,
                        bs.SO,
                        bs.Stint,
                        bs.Triples
                    }).ToList();

                foreach (var itm in s)
                {
                    this.AB.Add(itm.AB);
                    this.BB.Add(itm.BB);
                    this.CS.Add(itm.CS);
                    this.Doubles.Add(itm.Doubles);
                    this.G.Add(itm.G);
                    this.GB.Add(itm.G_Batting);
                    this.GIDP.Add(itm.GIDP);
                    this.H.Add(itm.H);
                    this.HBP.Add(itm.HBP);
                    this.HR.Add(itm.HR);
                    this.IBB.Add(itm.IBB);
                    this.R.Add(itm.R);
                    this.RBI.Add(itm.RBI);
                    this.SB.Add(itm.SB);
                    this.SF.Add(itm.SF);
                    this.SH.Add(itm.SH);
                    this.SO.Add(itm.SO);
                    this.Stint.Add(itm.Stint);
                    this.Triples.Add(itm.Triples);
                    this.Year.Add(itm.Yr);
                    this.Teams.Add(new UriLink(String.Format("{0}team/{1}", Constants.baseUrl, itm.FieldedTeamId), string.Format("{0} {1}", itm.Yr, itm.NickName)));
                }

                var pi = (from p in context.People
                          where p.PersonId == playerid
                          select p).FirstOrDefault();
                this.PlayerItem = new UriLink(string.Format("{0}player/{1}", Constants.baseUrl, pi.PersonId), string.Format("{0} {1}", pi.FirstName, pi.LastName));

                return this;
            }
        }
       
    }
}